package com.bgs.travelapp.order.service.impl;
import com.bgs.travelapp.order.entity.OrderRealEntity;
import com.bgs.travelapp.order.entity.OrderStatusEntity;
import com.bgs.travelapp.order.mapper.OrderRealMapper;
import com.bgs.travelapp.order.mapper.OrderStatusMapper;
import com.bgs.travelapp.result.CommonResult;
import com.bgs.travelapp.userRealBinds.entity.UserRealBindsEntity;
import com.bgs.travelapp.userRealBinds.mapper.UserRealBindsMapper;
import com.bgs.travelapp.util.RanNum;
import com.bgs.travelapp.util.RedisKeyExpirationListener;
import com.bgs.travelapp.util.RedisUtil;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.bgs.travelapp.order.entity.ScenicOrderEntity;
import com.bgs.travelapp.order.mapper.ScenicOrdersMapper;
import com.bgs.travelapp.order.service.ScenicOrdersService;
import com.bgs.travelapp.page.MyPageHelper;
import com.bgs.travelapp.page.PageRequest;
import com.bgs.travelapp.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
@Service
public class ScenicOrdersServiceImpl implements ScenicOrdersService {

    @Autowired
    ScenicOrdersMapper scenicOrdersMapper;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    RedisKeyExpirationListener redisKeyExpirationListener;

    @Autowired
    OrderStatusMapper orderStatusMapper;

    @Autowired
    OrderRealMapper orderRealMapper;

    @Autowired
    UserRealBindsMapper userRealBindsMapper;

    /**
     * 分页全查景区景点订单信息（前端管理展示景点订单信息）
     * fys 2022年8月4日11:02:47
     * lzy 2022-8-23 11点13分
     * @param pageRequest
     * @param loginName
     * @param orderStatus
     * @return
     */
    @Override
    public PageResult getScenicOredr(PageRequest pageRequest, String loginName, Integer orderStatus) {
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(ScenicOrderEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginName", loginName);
        if (orderStatus != 0 && !orderStatus.equals(0)) {
            criteria.andEqualTo("orderStatus", orderStatus);
        }
        example.setOrderByClause("purchase_time desc");
        List<ScenicOrderEntity> list = scenicOrdersMapper.selectByExample(example);
        PageResult pageEntity = MyPageHelper.pageInfo(list);
        return pageEntity;
    }

    /**
     * 分页查询景点订单信息（后端管理）
     * fys 2022年8月16日14:52:47
     * @param pageRequest
     * @param loginName
     * @return
     */
    @Override
    public PageResult getScenicOrderByPage(PageRequest pageRequest, String loginName) {
        Example example=new Example(ScenicOrderEntity.class);
        example.createCriteria().andLike("loginName","%"+loginName+"%");
        MyPageHelper.startPage(pageRequest);
        List<ScenicOrderEntity> list = scenicOrdersMapper.selectByExample(example);
        PageResult pageEntity = MyPageHelper.pageInfo(list);
        return pageEntity;
    }
    /**
     * 后端新增订单
     * fys 2022.8.17 15:55
     * @param scenicOrderEntity
     * @return
     */
    @Override
    public Integer saveScenicOrder(ScenicOrderEntity scenicOrderEntity) {
        String code = RanNum.createRanNum();
        scenicOrderEntity.setPurchaseTime(new Date());
        scenicOrderEntity.setEndTime(new Date());
        scenicOrderEntity.setOrderNo(code);
        return scenicOrdersMapper.insert(scenicOrderEntity);
    }

    /**
     * 获取订单状态列表（前台展示）
     * lzy 2022-8-23 11点33分
     * @return
     */
    @Override
    public CommonResult getOrderStatusList() {
        OrderStatusEntity orderStatusEntity = new OrderStatusEntity();
        orderStatusEntity.setDelFlag(0);
        List<OrderStatusEntity> orderStatusEntities = orderStatusMapper.select(orderStatusEntity);
        return CommonResult.ok(orderStatusEntities);
    }

    /**
     * 根据Id获取订单号（前台展示）
     * lzy 2022-8-23 19点24分
    /**
     * 后端修改订单
     * fys 2022.8.17 15:55
     * @param scenicOrderEntity
     * @return
     */
    @Override
    public Integer updateScenicOrder(ScenicOrderEntity scenicOrderEntity) {
        Integer id = scenicOrdersMapper.updateByPrimaryKeySelective(scenicOrderEntity);
        if(id>0){
            System.out.println("修改成功");
        }else{
            System.out.println("添加失败");
        }
        return id;
    }
    /**
     * 删除订单（后端）（del永久删除）
     * @param id
     * @return
     */
    @Override
    public CommonResult getScenicOrderById(Integer id) {
        ScenicOrderEntity scenicOrderEntity = new ScenicOrderEntity();
        scenicOrderEntity.setId(id);
        ScenicOrderEntity scenicOrderEntity1 = scenicOrdersMapper.selectOne(scenicOrderEntity);
        return CommonResult.ok(scenicOrderEntity1);
    }

    /**
     * 获取该订单的购票人（前台展示）
     * lzy 2022-8-23 10点15分
     * @param sOId
     * @return
     */
    @Override
    public CommonResult getUserBindsListByOrderId(Integer sOId) {
        OrderRealEntity orderRealEntity = new OrderRealEntity();
        orderRealEntity.setSOId(sOId);
        // 根据订单id 查询 实名信息表 与 景区订单表 关联表 获取 该订单的所有购买人的实名信息
        List<OrderRealEntity> orderRealEntityList = orderRealMapper.select(orderRealEntity);
        // 用于存放购买人的实名信息
        ArrayList<Object> userRealBindsList = new ArrayList<>();
        UserRealBindsEntity userRealBindsEntity = new UserRealBindsEntity();
        // 遍历购买人 实名信息的 id
        for (OrderRealEntity orderReal : orderRealEntityList) {
            userRealBindsEntity.setId(orderReal.getURId());
            // 根据购买人的实名信息 id 查询该 实名信息
            UserRealBindsEntity userRealBinds = userRealBindsMapper.selectOne(userRealBindsEntity);
            // 将购买人的实名信息存放到 集合当中
            userRealBindsList.add(userRealBinds);
        }
        return CommonResult.ok(userRealBindsList);
    }

    @Override
    public Integer delScenicOrder(Integer id) {
        return scenicOrdersMapper.deleteByPrimaryKey(id);
    }

}
